package co.edu.udea.iw.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import co.edu.udea.iw.dao.SucursalDAO;
import co.edu.udea.iw.dto.sucursal;
import co.edu.udea.iw.exception.waDaoException;

public class SucursalDAOHibernate extends HibernateDaoSupport implements SucursalDAO {

	

	
	@Override
	public sucursal insertar(sucursal Sucursal) throws waDaoException {
		Transaction tx = null;
		Session session = null;
		try {
			session = getSession();
			tx = session.beginTransaction();
			session.save(Sucursal);
			tx.commit();

		} catch (HibernateException e) {

			throw new waDaoException(e);
		} finally {
			session.close();
		}
		return Sucursal;

	}

	@Override
	public sucursal modificar(sucursal Sucursal) throws waDaoException {

		Transaction tx = null;
		Session session = null;
		try {
			session = getSession();
			tx = session.beginTransaction();
			session.update(Sucursal);
			tx.commit();

		} catch (HibernateException e) {

			throw new waDaoException(e);
		} finally {
			session.close();
		}
		return Sucursal;
	}

	@Override
	public void eliminar(sucursal Sucursal) throws waDaoException {
		
		Transaction tx = null;
		Session session = null;
		try {
			session = getSession();
			tx = session.beginTransaction();
			
			session.delete(Sucursal);
			tx.commit();

		} catch (HibernateException e) {

			throw new waDaoException(e);
		} finally {
			session.close();
		}
	
	}

	@Override
	public List<sucursal> Consultar() throws waDaoException {
		List<sucursal> sucursales = null;
		Session session = null;
		try {
			 session = getSession();

			Criteria criteria = session.createCriteria(sucursal.class);
			sucursales = criteria.list();
		} catch (HibernateException e) {

			throw new waDaoException(e);
		}
		return sucursales;

	}

	@Override
	public sucursal obtener(String id_sucursal) throws waDaoException {
		sucursal Sucursal = null;
		Session session = null;
		try {
			 session = getSession();
			Sucursal = (sucursal) session.load(sucursal.class, id_sucursal);
		} catch (HibernateException e) {

			throw new waDaoException(e);
		}finally {
			session.close();
		}
		return Sucursal;
	}

}
